package com.wmwj.hxlj.service.impl;

import com.wmwj.hxlj.entity.Category;
import com.wmwj.hxlj.entity.vo.AdminCategoryVo;
import com.wmwj.hxlj.entity.vo.CategoryResult;
import com.wmwj.hxlj.entity.vo.CategoryVo;
import com.wmwj.hxlj.entity.vo.PageResult;
import com.wmwj.hxlj.mapper.CategoryMapper;
import com.wmwj.hxlj.service.CategoryService;
import com.wmwj.hxlj.utils.BeanCopyUtils;
import com.wmwj.hxlj.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceimpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Result<CategoryResult> getCategoryList() {


        //获取等级为1的分类列表
        List<Category> level1Categories = categoryMapper.findLevel1ByParentId();
        if (level1Categories == null || level1Categories.isEmpty()) {
            return Result.error("没有顶级分类");
        }
        //封装等级为1的分类列表为CategoryVo
        List<AdminCategoryVo> level1CategoryVo = BeanCopyUtils.copyBeanList(level1Categories, AdminCategoryVo.class);

        //获取等级为2的分类列表
        List<Category> level2Categories = categoryMapper.findLevel2ByParentId();

        if (level2Categories == null || level2Categories.isEmpty()){
            return Result.error("没有次级分类");
        }
        //封装等级为2的分类列表为CategoryVo
        List<AdminCategoryVo> level2CategoryVo = BeanCopyUtils.copyBeanList(level2Categories, AdminCategoryVo.class);

        CategoryResult<AdminCategoryVo> level1CategoryResult = new CategoryResult<>(level1CategoryVo, level2CategoryVo);
        //获取所有分类总数

        return Result.success(level1CategoryResult);
    }

    @Override
    public Result<String> addCategory(Category category) {
        //查询类名
        Category categoryByName = categoryMapper.findByName(category.getName());
        if (categoryByName != null) {
            throw new RuntimeException("类名已存在");
        }

        //设置时间
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());

        try{
            int insert = categoryMapper.insert(category);
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw  new RuntimeException("添加失败");
        }

    }

    @Override
    public Result<String> deleteCategory(Integer id) {
        if (categoryMapper.countChildren(id) > 0) {
//            return Result.error("该分类下存在子分类，无法删除");
            throw new RuntimeException("该分类下存在子分类，无法删除");
        }

        try {
            categoryMapper.deleteById(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public Result<String> updateCategory(Category category) {
        if (category == null || category.getId() == null) {
            return Result.error("分类信息不完整");
        }
        // 检查分类是否存在
        Category existingCategory = categoryMapper.findById(category.getId());
        if (existingCategory == null) {
            return Result.error("分类不存在");
        }
        // 更新分类信息
        int result = categoryMapper.update(category);
        return result > 0 ? Result.success("更新成功") : Result.error("更新失败");
    }

    @Override
    public Result<PageResult<AdminCategoryVo>> adminGetAllCategory(Integer pageStart) {
        int pageStarts = (pageStart - 1) * 10;
        int pageSize = 10;
        List<Category> categories = categoryMapper.findAllEnabled(pageStarts , pageSize);
        if (categories == null || categories.isEmpty()) {
            return Result.error("没有分类");
        }
        List<AdminCategoryVo> adminCategoryVos = BeanCopyUtils.copyBeanList(categories, AdminCategoryVo.class);

        int total = categoryMapper.count();
        PageResult<AdminCategoryVo> pageResult = new PageResult<>(adminCategoryVos, total);
        return Result.success(pageResult);
    }

}
